package com.qdsgjsfk.vision.base;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.qdsgjsfk.vision.dao.GreenDaoManager;
import com.qdsgjsfk.vision.service.SocketService;
import com.qdsgjsfk.vision.util.PackageUtils;
import com.rest.constant.Constant;
import com.tencent.bugly.Bugly;
import com.tencent.stat.StatConfig;
import com.tencent.stat.StatService;

import org.litepal.LitePal;

import io.reactivex.annotations.NonNull;


/**
 * Created by Administrator on 2018/1/9.
 */

public class BaseApplication extends Application {

    public final static String API_KEY = "D65E46E581434AAF8EFCB5F6A6D9916C";
    public final static String API_SECRET = "3065B07556A34A76A233AE0E2899878F";
    public final static String APP_KEY = "6UHA0oCNrQcUflib7UuOXAYCTsMis0kMrQGp";
    public final static String APP_SECRET = "PJ3JRixfdD70jE6b2Ef5YiaX6PxTpbYLyPjZ";
    public static int userType; //1:基层医生 2：中心医院医生

    public static String strName = "";
    public static String strAddress = "";
    @SuppressLint("StaticFieldLeak")
    public static Context context;
    public static boolean isPad = false;
    public static boolean permission = false;
    public static boolean addChinaNoId = false;
    public static boolean addUnChina = false;
    public static boolean faceAdd = false;
    public static boolean faceSearch = false;
    public static boolean isShowOther = false;//是否有其他项配置
    public static boolean isShowAxis = false;//是否有眼轴项配置
    public static int currentTab = 0;
    public static int currentDistance = 5;
    public static String currentEditId;
    public static String currentTaskId;
    private int mFinalCount;
    public static BleDevice currentBleDevice;
    public static int lastActivity;//0 TypeActivity  1  StudentDetailActivity  2  other
    public static String studentId, taskId;
    public static int lastFragment = 0;// 0 视力 1 屈光 2 眼轴
    public static String localVersion;// 本地安装版本r
    public static boolean isHandheldScreenerConnected;// 是否手持筛查仪已连接

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        LitePal.initialize(this);
        GreenDaoManager.getInstance();
        isPad = isPad(this);
        localVersion = PackageUtils.getVersionName(context);
        Bugly.init(getApplicationContext(), "eab3877040", false);
        StatConfig.setDebugEnable(false);
        // 基础统计API
        StatService.registerActivityLifecycleCallbacks(this);

        this.registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                if (mFinalCount == 0) {
                    Log.e("onActivity-----", "onActivityStarted");
                    stopService(new Intent(context, SocketService.class));
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        startForegroundService(new Intent(context, SocketService.class));
                    } else {
                        startService(new Intent(context, SocketService.class));
                    }
                }
                mFinalCount++;
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                mFinalCount--;
                //说明从前台回到了后台
                if (mFinalCount == 0) {
                    Log.e("onActivity-----", "onActivityStopped");
                }
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });

        BleManager.getInstance().init(this);
        BleManager.getInstance()
                .enableLog(true)
                .setReConnectCount(1, 5000)//重连次数以及间隔时间
                .setSplitWriteNum(20)//拆分写入数
                .setConnectOverTime(10000)//连接超时时间
                .setOperateTimeout(5000);//操作超时时间
    }

    /**
     * * 判断当前设备是手机还是平板，代码来自 Google I/O App for Android
     * * @param context
     * * @return 平板返回 True，手机返回 False
     *
     * @time 2020/4/27 18:03
     */
    public boolean isPad(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }


    /**
     2  *                             _ooOoo_
     3  *                            o8888888o
     4  *                            88" . "88
     5  *                            (| -_- |)
     6  *                            O\  =  /O
     7  *                         ____/`---'\____
     8  *                       .'  \\|     |//  `.
     9  *                      /  \\|||  :  |||//  \
     10  *                     /  _||||| -:- |||||-  \
     11  *                     |   | \\\  -  /// |   |
     12  *                     | \_|  ''\---/''  |   |
     13  *                     \  .-\__  `-`  ___/-. /
     14  *                   ___`. .'  /--.--\  `. . __
     15  *                ."" '<  `.___\_<|>_/___.'  >'"".
     16  *               | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     17  *               \  \ `-.   \_ __\ /__ _/   .-` /  /
     18  *          ======`-.____`-.___\_____/___.-`____.-'======
     19  *                             `=---='
     20  *          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     21  *                     佛祖保佑        永无BUG
     22  *            佛曰:
     23  *                   写字楼里写字间，写字间里程序员；
     24  *                   程序人员写程序，又拿程序换酒钱。
     25  *                   酒醒只在网上坐，酒醉还来网下眠；
     26  *                   酒醉酒醒日复日，网上网下年复年。
     27  *                   但愿老死电脑间，不愿鞠躬老板前；
     28  *                   奔驰宝马贵者趣，公交自行程序员。
     29  *                   别人笑我忒疯癫，我笑自己命太贱；
     30  *                   不见满街漂亮妹，哪个归得程序员？
     31 */
}

